package net.zoneland.knowledge.utils;

import com.alibaba.excel.write.handler.impl.DefaultRowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * 序号自增长.
 * @author zx
 */
public class AddNoHandler extends DefaultRowWriteHandler {

    private CellStyle headerStyle;

    private CellStyle contentStyle;

    @Override
    public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
                               Integer relativeRowIndex, Boolean isHead) {
        //给表格头样式赋值
        if (headerStyle == null) {
            final Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
            headerStyle = CellStyleUtil.getHeaderStyle(workbook);
        }
        //给表格内容样式赋值
        if (contentStyle == null) {
            final Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
            contentStyle = CellStyleUtil.getContentStyle(workbook);
        }

        // 在行创建完成后添加序号列
        final Cell cell = row.createCell(0);
        int rowNum = row.getRowNum();
        if (rowNum == 0) {
            cell.setCellValue("序号");
            cell.setCellStyle(headerStyle);
        } else {
            cell.setCellValue(rowNum);
            cell.setCellStyle(contentStyle);
        }
    }

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
                                Integer relativeRowIndex, Boolean isHead) {
    }

}
